const api = require("../../../utils/api.js");
const app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false

  },
    /**
  * 控制 pop 的打开关闭
  * 该方法作用有2:
  * 1：点击弹窗以外的位置可消失弹窗
  * 2：用到弹出或者关闭弹窗的业务逻辑时都可调用
  */
 toggleDialog() {
  this.setData({
    isLogin: !this.data.isLogin
  });

},
  //获取页面初始化数据
  gethfData:function(that){
    let userData = wx.getStorageSync("USERDATA");
    let courseInfo = wx.getStorageSync("COURSEINFO");
    let url = "/study/getUnitlist";
    let parmas ={
      uid:userData.uid,
      sessionid:userData.sessionid,
      market:app.market,
      courseid:courseInfo.id,
      type:5,
      isPageing:0
    };
    api.sendPosts(url,parmas,function(res){
      console.log(res);
      // that.setData({
      //   unitlist:res.unitlist
      // });
    });
  },
  // 跳转去免费领界面
buyPage:function(){
  wx.navigateTo({
    url: '/group_admin/pages/goPage/goPage',
  })
},
// 用户点击锁题 弹出购买界面
buyBook:function(){
  var that=this;
  // 跳转去免费领界面
  if(this.data.buy_button=="领资料"){
    that.buyPage();
  }else if(this.data.buy_button=="购买"){
    var loginSuccess= wx.getStorageSync("loginSuccess");
    if(!loginSuccess){
      that.setData({
        isLogin:true
      }) 
    }else{
      that.setData({
        "buyDialog":true
      })
    }
  }
},
 // 微信授权
 wxsq: function() {

  wx.showLoading({
    title: '加载中',
  })
  setTimeout(function () {
    wx.hideLoading()
  }, 2000)

  var that=this;
  api.weChatLogin(this,function(res){
    console.log("res--------------"+res);
     //将用户的sessionid uid 保存在缓存中
     console.log("我的我页面中登陆得返回值");
    if(res){
      that.setData({
        hasUserInfo: true,
        userInfo:wx.getStorageSync('USERINFO'),
        isLogin:false
      })
      wx.setStorageSync('loginSuccess', true);
    } else{
      wx.setStorageSync('loginSuccess', false)
      that.setData({
        isLogin:false
      })
      wx.showToast({
        title: '授权异常',
      })
    }
    that.onShow();
  })
},
//用户点击确认购买 跳转到确认订单界面
confirmBuy:function(e){
  var that=this;
  var type=e.currentTarget.dataset.type;
  if(this.data.buy_button=="购买"){
    var loginSuccess= wx.getStorageSync("loginSuccess");
    if(!loginSuccess){
      that.setData({
        isLogin:true
      }) 
    }else{
      wx.navigateTo({
        url: '/group_admin/pages/confirmOrder/confirmOrder?module='+type+'&flagClass=1'
      })
    }
  }else if(this.data.buy_button=="领资料"){
    that.goMoneyPage();
  }
},
// 没有数据 用户点击立即购买
getBuy:function(){
  wx.navigateTo({
    url: '/group_admin/pages/confirmOrder/confirmOrder?module='+type+'&flagClass=1'
  })
},
  // 免费领资料
  goMoneyPage:function(){
    wx.navigateTo({
      url: '/group_admin/pages/goPage/goPage',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //获取页面初始化数据
    that.gethfData(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //根据操作系统版本显示购买/领资料按钮
    var result = wx.getSystemInfoSync();
    var devsystem = result.system;
    var subsys = devsystem.substring(0, 3);
    console.log(result);
    if (subsys == "iOS") {
      this.setData({
        "buy_button": "领资料"
      });
    } else {
      this.setData({
        "buy_button": "购买"
      });
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var courseInfo= wx.getStorageSync("COURSEINFO");
    var MODULELIST=wx.getStorageSync('MODULELIST');
    var moduleType=MODULELIST.module;
    // var moduleType=this.data.module;


    // 章节分类 type（1正常章节，2押题,3：模拟测试，4：历年真题，5：高频题库，6：新版考前押题，7：热门试卷）
    var type=5;

    var params = {
      "sessionid": resYtkUserInfo.sessionid,
      "uid": resYtkUserInfo.uid,
      "courseid": courseInfo.id,
      "market": getApp().globalData.market,
      "type": type
    };
    this.setData({
      "type":type
    })
    var url = "/study/getUnitlist";
    var that = this;
    api.sendPosts( url,params, function(res) {
        that.setData({
          "highFreList": res,
          });
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})